﻿using Api.Core.IRepository.IUnitOfWork;
using Api.Core.IRepository.User;
using Api.Core.Model.Entity;
using Api.Core.Model.Table;
using Api.Core.Model.User;
using Api.Core.Model.ViewModel;
using AutoMapper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Api.Core.Repository.User
{
    /// <summary>
    /// 
    /// </summary>
    public class Userepository : BaseRepository<UserTable>, IUserRepository
    {
        public ISugarUnitOfWork suger;
        public IMapper mapper;
        public Userepository(ISugarUnitOfWork sugarUnitOfWork,IMapper _mapper) : base(sugarUnitOfWork)
        {
            suger = sugarUnitOfWork;
            mapper = _mapper;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<List<UserEntity>> GetUserGroup(UserViewModel model)
        {
            using (var context = suger.GetDbClient())
            {
                var result = context.Queryable<UserTable, UserGroupTable, GroupTable>((u, ug, g) => new object[] {
                    JoinType.Left,u.UserId ==ug.UserId,
                    JoinType.Left,ug.GroupId ==g.GroupId
                }).WhereIF(!string.IsNullOrWhiteSpace(model.UserId), u => u.UserId == model.UserId)
                .WhereIF(!string.IsNullOrWhiteSpace(model.UserName), u => u.UserName.Contains(model.UserName))
                .Select((u, ug, g) =>
                  new UserEntity()
                  {
                      UserId = u.UserId,
                      UserName = u.UserName,
                      Status = u.Status,
                      GroupId = ug.GroupId,
                      GroupName = g.GroupName
                  });
                return await result.ToListAsync();
            }
        }

        #region 新增
        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> AddUserWithGroup(UserEntity model)
        {
            var result = false;
            using (var context = suger.GetDbClient())
            {
                try
                {
                    context.BeginTran();
                    var u = context.Insertable(mapper.Map<UserTable>(model));
                    var s1 = await u.ExecuteCommandAsync();
                    List<UserGroupTable> list = new List<UserGroupTable>();
                    foreach (var group in model.GroupIds)
                    {
                        UserGroupTable item = new UserGroupTable();
                        item.GroupId = group;
                        item.UserId = model.UserId;
                        list.Add(item);
                    }
                    var ug = context.Insertable(list);
                    var s2 = await ug.ExecuteCommandAsync();
                    if (s1 > 0 && s2 > 0)
                    {
                        result = true;
                        context.CommitTran();
                    }
                    else
                    {
                        context.RollbackTran();
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
                finally
                {
                    context.RollbackTran();
                }
            }
            return result;

        }

 
        #endregion
    }
}
